package org.puppet.game.control;

import org.puppet.game.EventCode;
import org.puppet.game.World;
import org.puppet.game.element.Puppet;
import org.puppet.game.map.SceneMap;

public class PuppetUpdateController extends AbstractController {

	public final static int IN_WORLD = EventCode.code++;
	
	private long mMoveDelay = 10;
	private long mLastMove;

    private Puppet puppet;
	
	public PuppetUpdateController(Puppet puppet) {
		this.puppet = puppet;
	}

	public boolean action(Integer eventId, Object e) {
		long now = System.currentTimeMillis();
		
		if (now - mLastMove >= mMoveDelay) {
			float[] deltaPosition= puppet.getDeltaPosition(mMoveDelay);
			float dx=deltaPosition[0], dy=deltaPosition[1];
			SceneMap map=World.current.getCurrentView().getSceneMap();
			if (puppet.x+dx>250 ){
				puppet.x=250;
				map.scrollBy(0-dx, 0);
			}else if (puppet.x+dx<20 ){
				puppet.x=20;
				map.scrollBy(0-dx, 0);
			}else{
				puppet.x += dx;
			}
			puppet.y -= dy;
			puppet.update(mMoveDelay);
			World.current.broadcast(IN_WORLD, puppet);
			mLastMove = now;
		}
		return true;
	}

	public int[] lookEvent() {
		return new int[]{EventCode.UPDATE};
	}

}
